package com.javajun.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.javajun.entity.Claxx;
import com.javajun.entity.MainCourse;
import com.javajun.entity.Manager;
import com.javajun.form.ClaxxForm;
import com.javajun.service.ClaxxService;
import com.javajun.service.ManagerService;

/**
 * @ClassName: ClaxxController
 * @Description: claxx班级的控制器
 * @author: xlm
 * @date: 2018年10月15日
 */
@Controller
@RequestMapping(value="/admin/claxx")
public class ClaxxController {

	@Autowired
	private ClaxxService claxxService;
	@Autowired
	private ManagerService managerService;
	
	@RequestMapping(value="/manage")
	public String Manage(ModelMap map, ClaxxForm form) {
		List<Claxx> models = new ArrayList<>();
		models = claxxService.findAll(claxxService.BuildSpec(form));
		/*for (Claxx model : models) {
			System.out.println("班级名字："+model.getName());
			for (MainCourse mainCourse : model.getMainCourses()) {
				System.out.println("班级选课ID：" + mainCourse.getId() + "，班级主修课名称："+mainCourse.getName());
			}
		}*/
		// 所有班主任
		List<Manager> managers = managerService.findAll();
		map.put("managers", managers);
		// 搜索条件
		map.put("searchs", form);
		// 所有班级
		map.put("models", models);
		return "claxx/manage";
	}
	
	// 修改页面
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public String Edit(ModelMap map, Integer id) {
		Claxx model = new Claxx();
		if(id != null) {
			model = claxxService.findById(id);
		}
		// 因为修改班级页面需要选择班主任，所以将所有班主任的集合传递到页面
		List<Manager> models = new ArrayList<>();
		models = managerService.findAll();
		map.put("model", model);
		map.put("models", models);
		return "claxx/edit";
	}
	
	// 保存方法
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String Save(ModelMap map, Claxx entity) {
		Claxx claxx = new Claxx();
		if(entity.getId() != null) {
			claxx = claxxService.findById(entity.getId());
		}
		if(entity.getName() != null) {
			claxx.setName(entity.getName());
		}
		if(entity.getManager().getId() != null) {
			Manager manager = managerService.findById(entity.getManager().getId());
			claxx.setManager(manager);
		}
		claxxService.save(claxx);
		return "redirect:manage";
	}
	
	// 新增页面
	@RequestMapping(value="/add")
	public String Add(ModelMap map) {
		// 因为修改班级页面需要选择班主任，所以将所有班主任的集合传递到页面
		List<Manager> models = new ArrayList<>();
		models = managerService.findAll();
		map.put("models", models);
		return "claxx/add";
	}
	
	// 删除一个班级
	@RequestMapping(value="/delete")
	public String Delete(ModelMap map, Integer id) {
		if(id != null) {
			Claxx entity = claxxService.findById(id);
			claxxService.delete(entity);
		}
		return "redirect:manage";
	}
	
	/*public String ClaxxMainCourse(ModelMap map) {
	}*/
}
